package com.cn.kabalifeuser.ui.service.adapter;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.ui.service.bean.ShopInfoBean;
import com.cn.kabalifeuser.utils.GlideEngine;
import com.cn.kabalifeuser.utils.GlideUtil;
import com.cn.kabalifeuser.view.xx.MyRatingBar;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 晓林 on 2020/9/15.
 * function:
 */
public class DetailsCommentListAdapter extends BaseQuickAdapter<ShopInfoBean.ResultBean.CommentBean, BaseViewHolder> {

    Activity activity;

    public DetailsCommentListAdapter(int layoutResId, @Nullable List<ShopInfoBean.ResultBean.CommentBean> data, Activity activity) {
        super(layoutResId, data);
        this.activity = activity;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, ShopInfoBean.ResultBean.CommentBean item) {
        GlideUtil.ImageLoad(this.mContext, item.getUser().getHead(), helper.getView(R.id.img_head));
        MyRatingBar ratingbar = helper.getView(R.id.ratingbar);


        ratingbar.setRating(TextUtils.isEmpty(item.getFraction()) ? 0 : Float.parseFloat(item.getFraction()));

        ratingbar.setEnabled(false);
        helper.setText(R.id.tv_nickname, item.getUser().getNickname())
                .setText(R.id.tv_fraction, item.getFraction())
                .setText(R.id.tv_content, item.getContent())
                .setText(R.id.tv_type, "服务类型:" + item.getCategory())
                .setText(R.id.tv_time, item.getCreated_at());

        if (item.getImg() != null && item.getImg().size() > 0) {
            helper.setGone(R.id.rv_pics, true);
            RecyclerView rvPics = helper.getView(R.id.rv_pics);
            rvPics.setLayoutManager(new GridLayoutManager(this.mContext, 3));
            rvPics.setNestedScrollingEnabled(false);
            //创建适配器
            PisListAdapter mAdapterPic = new PisListAdapter(R.layout.pics_list_pic_item, item.getImg());
            //给RecyclerView设置适配器
            rvPics.setAdapter(mAdapterPic);
            List<LocalMedia> selectList = new ArrayList<>();
            for (String str : item.getImg()) {
                LocalMedia localMedia = new LocalMedia();
                localMedia.setPath(str);
                selectList.add(localMedia);
            }
            mAdapterPic.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    PictureSelector.create(activity)
                            .themeStyle(R.style.picture_default_style) // xml设置主题
                            //.setPictureStyle(mPictureParameterStyle)// 动态自定义相册主题
                            //.setPictureWindowAnimationStyle(animationStyle)// 自定义页面启动动画
                            .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)// 设置相册Activity方向，不设置默认使用系统
                            .isNotPreviewDownload(true)// 预览图片长按是否可以下载
                            .loadImageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                            .openExternalPreview(position, selectList);
                }
            });
        } else helper.setGone(R.id.rv_pics, false);
    }
}
